/*
 * 保存物料品类交易
 * 参数   交易码：trancode=12163
 * 
 */

package org.ccams.tran;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.ccams.domain.TGoodskind;
import org.json.JSONArray;
/**
 * 保存物料品类交易
 * @author liuguangshuai@gmail.com
 * 系统名称：定制化辅助生产制造系统
 * 时间：Oct 15, 2008
 */
public class Tran12163 extends AbstractJsonTran {

	@SuppressWarnings("unchecked")
	public void buildJSONObject(Map model, JSONObject json,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		String params = (String) request.getParameter("json");
		JSONArray Companys = new JSONArray(params);
		HashMap<String, Object> jsonMap = new HashMap<String, Object>();
		boolean success = false;
		String message = "";
		JSONArray newCompanys = new JSONArray();
		int k = 0;
		for(int i=0;i<Companys.length();i++){
			org.json.JSONObject company = Companys.optJSONObject(i);
			if(company.has("idtGoodskind")){
				TGoodskind t = new TGoodskind();
				t.setGrade(company.getInt("grade"));
				t.setIdtGoodskind(company.getInt("idtGoodskind"));
				t.setKindname(company.getString("kindname"));
				t.setKindno(company.getString("kindno"));
				k = getCcamsService().getTgoodskindDao().updateByPrimaryKeySelective(t);	//更新			
			}else{
				newCompanys.put(company);
			}			
		}
		for(int i=0;i<newCompanys.length();i++){
			org.json.JSONObject newCompany = Companys.optJSONObject(i);
			TGoodskind t = new TGoodskind();
			t.setGrade(newCompany.getInt("grade"));
			t.setKindname(newCompany.getString("kindname"));
			t.setKindno(newCompany.getString("kindno"));
			k = getCcamsService().getTgoodskindDao().insert(t);//插入
		}
		if (k == 1) {
			success = true;
			message = "品类保存成功";
		} else {
			success = false;
			message = "品类保存失败";
		}
		jsonMap.put("success", success);
		jsonMap.put("message", message);
		json.putAll(jsonMap);
	}
}
